function searchEquipment(session, str) {
	if (str.length == 0){ 
		str = "all";
	}
	
	var img = document.getElementById('loading-equipment');
	img.style.visibility = 'visible';
	
	var xmlhttp = new XMLHttpRequest();
	
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			img.style.visibility = 'hidden';
		
			var xmlDoc = parseXML(xmlhttp.responseText);
			
			console.debug("Here is the xml document: %o", xmlDoc);
			
			var equipments = xmlDoc.getElementsByTagName("xaresponse")[0].childNodes[1];
						
			if (equipments.nodeName != 'error') 
			{
				createEquipmentTable(equipments);
			} 
			else 
			{
				clearEquipmentInfo();
				
				var p = document.createElement('p');
				p.setAttribute('id', 'equipment-message');
				p.appendChild(document.createTextNode('No match found.'));
				
				document.getElementById('tabr2').appendChild(p);
			}
		}
	}

	xmlhttp.open("GET","../../apicommfe/search_equipment?session=" + session + "&query=" + str, true);
	xmlhttp.send();
}

function parseXML(xml) {
	var parser = new DOMParser();
	var xmlDoc = parser.parseFromString(xml,"text/xml");
	
	return xmlDoc;
}

function createEquipmentTable(equipments) {
	clearEquipmentInfo();

	var newTable = document.createElement('table');
	newTable.setAttribute('id', 'equipment-table');
	newTable.setAttribute('class', 'sortable');
	
	var attributes = new Array('ID', 'Equipment ID', 'Name', 'Description', 'Added');
	
	var tr = document.createElement('tr');
	
	for (var c = 0; c < attributes.length; c++) {
		var tn = document.createTextNode(attributes[c]);
		var td = document.createElement('td');
		td.appendChild(tn);
		tr.appendChild(td);
	}

	newTable.appendChild(tr);
	
	if (equipments.nodeName == 'equipmentlist')
	{
		for (var c = 0; c < equipments.childElementCount; c++)
		{
			var contact = document.createElement('tr');
			
			for (var a = 0; a < equipments.childNodes[c].childElementCount; a++) {
				var tn = document.createTextNode(equipments.childNodes[c].childNodes[a].childNodes[0].nodeValue);
				var td = document.createElement('td');
				td.appendChild(tn);
				contact.appendChild(td);
			}
			
			newTable.appendChild(contact);
		}
	} 
	else
	{
		var contact = document.createElement('tr');
	
		for (var a = 0; a < equipments.childElementCount; a++) 
		{
			var tn = document.createTextNode(equipments.childNodes[a].childNodes[0].nodeValue);
			var td = document.createElement('td');
			td.appendChild(tn);
			contact.appendChild(td);
		}
		
		newTable.appendChild(contact);
	}
	
	document.getElementById('tabr2').appendChild(newTable);	
}

function clearEquipmentInfo()
{
	var div   = document.getElementById('tabr2');
	
	var table = document.getElementById('equipment-table');
	var p     = document.getElementById('equipment-message'); 
	
	if (table != null) {
		div.removeChild(table);
	}
	
	if (p != null) {
		div.removeChild(p);
	}
}